(function ($) {

AjaxSolr.CurrentSearchWidget = AjaxSolr.AbstractWidget.extend({
  mappers : {},
  afterRequest: function () {
    var self = this;
    var links = [];

    var fq = this.manager.store.values('fq');
    for (var i = 0, l = fq.length; i < l; i++) {
      var split = fq[i].split(":");
      var value = split[split.length-1];
      var name = "";
      for(var j=0; j<split.length-1; j++) {
          if(j==0) name = split[0];
          else name = name+":"+split[j];
      }
      var t = this.mappers[name];
      if(t!=null) {
         t = t+":"+value;
      } else {
         t = name+":"+value;
      }
      links.push($('<a href="#"/>').html(t+"<img style='margin-left:2px;' src='img/remove.png'>").click(self.removeFacet(fq[i])));
    }

    /*
    if (links.length > 1) {
      links.unshift($('<a href="#"/>').text('remove all').click(function () {
        self.manager.store.remove('fq');
        self.manager.doRequest(0);
        return false;
      }));
    }
    */

    if (links.length) {
      AjaxSolr.theme('list_items', this.target, links);
      $(this.target).attr("class","selection-active");
      $(this.target).prepend("<h2>Selected</h2>");
    }
    else {
      $(this.target).attr("class","");
      $(this.target).html('');
    }
  },

  removeFacet: function (facet) {
    var self = this;
    return function () {
      if (self.manager.store.removeByValue('fq', facet)) {
        self.manager.doRequest(0);
      }
      return false;
    };
  }
});
    })(jQuery);